/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package services;

import dao.InstitutionJpaController;
import entities.Institution;
import entities.Country;
import java.util.List;

public class InstitutionService {
    private static InstitutionJpaController ic;
    private static CountryService cs;

    private InstitutionJpaController getJPAController()
    {
        if(ic == null)
            ic = new InstitutionJpaController();
        return ic;
    }

    private CountryService getCountryService()
    {
        if(cs == null)
            cs = new CountryService();
        return cs;
    }

    public boolean saveInstitution(String name, boolean isHighSchool , String countryName)
    {
        boolean ret = true;
        ic = getJPAController();
        cs = getCountryService();
        Country c = null;
        Institution ins = new Institution(0, name, isHighSchool);
        try
        {
            c = cs.getCountry(countryName);
            ins.setCountry(c);
            ic.create(ins);
        }
        catch (Exception e)
        {
            ret = false;
        }
        return ret;
    }
    public Institution getInstitution(String name)
    {
        ic = getJPAController();
        return ic.getInstitutionByName(name);
    }
    public Institution getInstitution(Integer idInstitution)
    {
        ic = getJPAController();
        return ic.findInstitution(idInstitution);
    }

    public boolean editInstitution(Institution c)
    {
        ic = getJPAController();
        try
        {
            ic.edit(c);
            return true;
        } catch (Exception e)
        {
            return false;
        }
    }

    public boolean deleteInstitution(Institution c)
    {
        ic = getJPAController();
        try
        {
            ic.destroy(c.getIdInstitution());
            return true;
        }
        catch (Exception e)
        {
            return false;
        }
    }

    public List<Institution> getAllInstitutions()
    {
        ic = getJPAController();
        try
        {
            return ic.findAll();
        } catch (Exception e)
        {
            return null;
        }
    }
}
